 //
//  DBWelcomeController.swift
//  大波微博swift
//
//  Created by 大波 on 16/2/29.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class DBWelcomeController: UIViewController {
    
    //头像底部约束
    var iconViewBottonConstraint: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        //先加载图片
        setIcon()
        
        //没有图片则从网络获取
        DBUserAccountViewModel.shareUserAccountViewModel.loadUserInfo { (error) -> () in
            
            if error != nil {
                
                print(error)
                
                return
            }
            
            self.setIcon()
        }
    }
    
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        setAnimated()
    }
    
    ///网络加载头像
    private func setIcon() {
        
        if let avatar_large = DBUserAccountViewModel.shareUserAccountViewModel.userAccount?.avatar_large {
            
            let URLString = NSURL(string: avatar_large)!
            
            self.iconView.sd_setImageWithURL(URLString, placeholderImage: UIImage(named: "avatar_default_big"))
        }
    }
    
    private func prepareUI() {
        
        view.addSubview(backgroundView)
        
        view.addSubview(iconView)
        
        view.addSubview(welcomeLabel)
        
//        backgroundView.translatesAutoresizingMaskIntoConstraints = false
//        
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        
//        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        addConstraint()
    }
    
    private func addConstraint() {
        
        //背景
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView" : backgroundView]))
//        
//        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[bgView]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["bgView" : backgroundView]))
        
        //snap
        backgroundView.snp_makeConstraints { (make) -> Void in
            
//            make.edges.equalTo(self.view)
            
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        //头像
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        //头像底部约束
//        iconViewBottonConstraint = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200)
//        
//        view.addConstraint(iconViewBottonConstraint!)
//        
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
//        
//        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 85))
        
        
        //snap
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view)
            
            make.bottom.equalTo(view.snp_bottom).offset(-200)
            
            make.width.height.equalTo(85)
            
            
//            make.centerX.equalTo(view)
//            make.bottom.equalTo(view.snp_bottom).offset(-160)
//            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        //文字
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 230))
//        
//        view.addConstraint(NSLayoutConstraint(item: welcomeLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 160))
        
        
        //snap
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.top.equalTo(view.snp_top).offset(230)
            
//            make.centerX.equalTo(iconView)
//            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    
    private func setAnimated() {
        
//        iconViewBottonConstraint?.constant = -(UIScreen.mainScreen().bounds.height - 200)
        
        
        /*
            这里动画应该是更新约束   用错方法
        */
//        iconView.snp_makeConstraints { (make) -> Void in
//            
////            make.bottom.equalTo(view.snp_bottom).offset(-(UIScreen.mainScreen().bounds.height - 200))
//            
//            make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.height - 160))
//        }
        

        iconView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(view.snp_bottom).offset(-(UIScreen.height - 200))
        }
        
        UIView.animateWithDuration(1.25, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    self.welcomeLabel.alpha = 1
                    
                    }, completion: { (_) -> Void in
                        
                        //动画完成后跳转到main控制器
                        AppDelegate.outSwitchMain(true)
                })
        }
    }
    
    //MARK: - 懒加载
    ///背景
    private lazy var backgroundView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    ///头像
    private lazy var iconView: UIImageView = {
        
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        iconView.layer.cornerRadius = 42.5
        
        iconView.layer.masksToBounds = true
        
        return iconView
    }()
    
    ///欢迎文字
    private lazy var welcomeLabel: UILabel = {
        
        let welcomeTitle = UILabel()
        
//        welcomeTitle.sizeToFit()
        
        welcomeTitle.text = "欢迎回来"
        
        welcomeTitle.alpha = 0
        
        welcomeTitle.contentMode = UIViewContentMode.Center
        
        return welcomeTitle
    }()
}
